package com.qf.controller;

import com.qf.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/11/19 10:52
 */

@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    /**
     * 存储key-object，不设置生存时间
     * @param key
     * @param value
     * @return
     */
    @RequestMapping(value = "/save2cache-object", method = RequestMethod.POST)
    boolean saveObject(@RequestParam(value = "key") String key,
                       @RequestParam(value = "value") Object value) {
        return cacheService.set(key, value);
    }

    /**
     * 通过key获取string的value
     * @param key
     * @return
     */
    @RequestMapping(value = "/get/{key}", method = RequestMethod.GET)
    String get(@PathVariable("key") String key){
        return (String) cacheService.get(key);
    }

    /**
     * 通过key获取object的value
     * @param key
     * @return
     */
    @RequestMapping(value = "/get-object/{key}", method = RequestMethod.GET)
    Object getObject(@PathVariable("key") String key){
        return cacheService.get(key);
    }

    /**
     * 根据多个key删除数据
     * @param keys
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    boolean del(@RequestParam("keys") String... keys){
        return cacheService.del(keys);
    }

    /**
     * 存储key-hash，不设置生存时间
     * @param key
     * @param map
     * @return
     */
    @RequestMapping(value = "/hmset/{key}", method = RequestMethod.POST)
    boolean hMSet(@PathVariable("key") String key,
                  @RequestBody Map map){
        return cacheService.hMSet(key,map);
    }

    /**
     * 获取key-hash
     * @param key
     * @return
     */
    @RequestMapping(value = "/hmget/{key}", method = RequestMethod.GET)
    public Map hMGet(@PathVariable("key") String key){
        return cacheService.hMGet(key);
    }

    /**
     * 获取key-hash的某个filed
     * @param key
     * @param filed
     * @return
     */
    @RequestMapping(value = "/hget/{key}/{filed}", method = RequestMethod.GET)
    public Object hGet(@PathVariable("key") String key,
                       @PathVariable("filed") String filed){
        return cacheService.hGet(key,filed);
    }

    /**
     * 存储key-string，并设置生存时间
     * @param key
     * @param value
     * @param expireSecond
     * @return
     */
    @RequestMapping(value = "/save2cache", method = RequestMethod.POST)
    boolean set(@RequestParam("key") String key,
                @RequestParam("value") Object value,
                @RequestParam("expireSecond") Long expireSecond){
        return cacheService.set(key, value, expireSecond);
    }

    /**
     * 存储key-string，不设置生存时间
     * @param key
     * @param value
     * @return
     */
    @RequestMapping(value = "/save2cache-persist", method = RequestMethod.POST)
    boolean saveCache(@RequestParam("key") String key,
                      @RequestParam("value") String value){
        return cacheService.set(key, value);
    }

    /**
     * 自增数据
     * @param key
     * @param delta 每次自增多少
     * @return
     */
    @RequestMapping(value = "/incr", method = RequestMethod.GET)
    Long incr(@RequestParam("key") String key,
              @RequestParam("delta") long delta){
        return cacheService.incr(key,delta);
    }

    /**
     * 存储key-set，不设置生存时间
     * @param key
     * @param value
     * @return
     */
    @RequestMapping(value = "/sadd", method = RequestMethod.POST)
    boolean sadd(@RequestParam("key") String key,
                 @RequestParam("value") String... value){
        return cacheService.sadd(key,value);
    }

    /**
     * 针对key设置生存时间
     * @param key
     * @param expireSecond
     * @return
     */
    @RequestMapping(value = "/expire", method = RequestMethod.POST)
    boolean expire(@RequestParam("key") String key,
                  @RequestParam("expireSecond") Long expireSecond){
        return cacheService.expire(key,expireSecond);
    }
}
